package com.jingdun.sport.common.util;

import sun.misc
        .BASE64Decoder;
import sun.misc.BASE64Encoder;

import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.geom.AffineTransform;
import java.awt.image.AffineTransformOp;
import java.awt.image.BufferedImage;
import java.awt.image.ImageObserver;
import java.io.*;
import java.util.Date;

public final class ImageUtils {
    private static BASE64Encoder encoder = new BASE64Encoder();
    private static BASE64Decoder decoder = new BASE64Decoder();
    private static final String PICTRUE_FORMATE_JPG = "jpg";

    private ImageUtils() {
    }

    public static final void pressImage(String targetImg, String waterImg, int x, int y, float alpha) {
        try {
            File file = new File(targetImg);
            Image image = ImageIO.read(file);
            int width = image.getWidth((ImageObserver) null);
            int height = image.getHeight((ImageObserver) null);
            BufferedImage bufferedImage = new BufferedImage(width, height, 1);
            Graphics2D g = bufferedImage.createGraphics();
            g.drawImage(image, 0, 0, width, height, (ImageObserver) null);
            Image waterImage = ImageIO.read(new File(waterImg));
            int width_1 = waterImage.getWidth((ImageObserver) null);
            int height_1 = waterImage.getHeight((ImageObserver) null);
            g.setComposite(AlphaComposite.getInstance(10, alpha));
            int widthDiff = width - width_1;
            int heightDiff = height - height_1;
            if (x < 0) {
                x = widthDiff / 2;
            } else if (x > widthDiff) {
                x = widthDiff;
            }

            if (y < 0) {
                y = heightDiff / 2;
            } else if (y > heightDiff) {
                y = heightDiff;
            }

            g.drawImage(waterImage, x, y, width_1, height_1, (ImageObserver) null);
            g.dispose();
            ImageIO.write(bufferedImage, "jpg", file);
        } catch (IOException var16) {
            var16.printStackTrace();
        }

    }

    public static void pressText(String targetImg, String pressText, String fontName, int fontStyle, int fontSize, Color color, float alpha) {
        try {
            File file = new File(targetImg);
            Image image = ImageIO.read(file);
            int width = image.getWidth((ImageObserver) null);
            int height = image.getHeight((ImageObserver) null);
            BufferedImage bufferedImage = new BufferedImage(width, height, 1);
            Graphics2D g = bufferedImage.createGraphics();
            g.drawImage(image, 0, 0, width, height, (ImageObserver) null);
            g.setFont(new Font(fontName, fontStyle, fontSize));
            g.setColor(color);
            g.setComposite(AlphaComposite.getInstance(10, alpha));
            int x = (int) ((double) width * 0.8D);
            int y = (int) ((double) height * 0.9D);
            int width_1 = fontSize * getLength(pressText);
            int widthDiff = width - width_1;
            int heightDiff = height - fontSize;
            if (x < 0) {
                x = widthDiff / 2;
            } else if (x > widthDiff) {
                x = widthDiff;
            }

            if (y < 0) {
                y = heightDiff / 2;
            } else if (y > heightDiff) {
                y = heightDiff;
            }

            g.drawString(pressText, x, y + fontSize);
            g.dispose();
            ImageIO.write(bufferedImage, "jpg", file);
        } catch (Exception var19) {
            var19.printStackTrace();
        }

    }

    public static int getLength(String text) {
        int textLength = text.length();
        int length = textLength;

        for (int i = 0; i < textLength; ++i) {
            if (String.valueOf(text.charAt(i)).getBytes().length > 1) {
                ++length;
            }
        }

        return length % 2 == 0 ? length / 2 : length / 2 + 1;
    }

    public static void resize(String filePath, int height, int width, boolean bb) {
        try {
            double ratio = 0.0D;
            File f = new File(filePath);
            BufferedImage bi = ImageIO.read(f);
            Image itemp = bi.getScaledInstance(width, height, 4);
            if (bi.getHeight() > height || bi.getWidth() > width) {
                if (bi.getHeight() > bi.getWidth()) {
                    ratio = (new Integer(height)).doubleValue() / (double) bi.getHeight();
                } else {
                    ratio = (new Integer(width)).doubleValue() / (double) bi.getWidth();
                }

                AffineTransformOp op = new AffineTransformOp(AffineTransform.getScaleInstance(ratio, ratio), (RenderingHints) null);
                itemp = op.filter(bi, (BufferedImage) null);
            }

            if (bb) {
                BufferedImage image = new BufferedImage(width, height, 1);
                Graphics2D g = image.createGraphics();
                g.setColor(Color.white);
                g.fillRect(0, 0, width, height);
                if (width == ((Image) itemp).getWidth((ImageObserver) null)) {
                    g.drawImage((Image) itemp, 0, (height - ((Image) itemp).getHeight((ImageObserver) null)) / 2, ((Image) itemp).getWidth((ImageObserver) null), ((Image) itemp).getHeight((ImageObserver) null), Color.white, (ImageObserver) null);
                } else {
                    g.drawImage((Image) itemp, (width - ((Image) itemp).getWidth((ImageObserver) null)) / 2, 0, ((Image) itemp).getWidth((ImageObserver) null), ((Image) itemp).getHeight((ImageObserver) null), Color.white, (ImageObserver) null);
                }

                g.dispose();
                itemp = image;
            }

            ImageIO.write((BufferedImage) itemp, "jpg", f);
        } catch (IOException var11) {
            var11.printStackTrace();
        }

    }

    public static void reduceImg(File srcfile, File imgdist, int widthdist, int heightdist, Float rate) {
        try {
            if (!srcfile.exists()) {
                return;
            }

            if (rate != null && rate > 0.0F) {
                int[] results = getImgWidth(srcfile);
                if (results == null || results[0] == 0 || results[1] == 0) {
                    return;
                }

                widthdist = (int) ((float) results[0] * rate);
                heightdist = (int) ((float) results[1] * rate);
            }

            Image src = ImageIO.read(srcfile);
            String formatName = srcfile.getName().substring(srcfile.getName().lastIndexOf(".") + 1);
            BufferedImage tag = new BufferedImage(widthdist, heightdist, 1);
            tag.getGraphics().drawImage(src.getScaledInstance(widthdist, heightdist, 4), 0, 0, (ImageObserver) null);
            ImageIO.write(tag, formatName, imgdist);
            tag.flush();
        } catch (IOException var8) {
            var8.printStackTrace();
        }

    }

    public static String base64StringToImage(String base64String) {
        try {
            byte[] bytes1 = decoder.decodeBuffer(base64String);
            ByteArrayInputStream bais = new ByteArrayInputStream(bytes1);
            BufferedImage bi1 = ImageIO.read(bais);
            String path = ImageUtils.class.getClassLoader().getResource("/tmp").getPath() + "/" + (new Date()).getTime() + ".png";
            File w2 = new File(path);
            ImageIO.write(bi1, "png", w2);
            return path;
        } catch (IOException var6) {
            var6.printStackTrace();
            return null;
        }
    }

    public static int[] getImgWidth(File file) {
        InputStream is = null;
        BufferedImage src = null;
        int[] result = new int[]{0, 0};

        try {
            is = new FileInputStream(file);
            src = ImageIO.read(is);
            result[0] = src.getWidth((ImageObserver) null);
            result[1] = src.getHeight((ImageObserver) null);
            is.close();
        } catch (Exception var5) {
            var5.printStackTrace();
        }

        return result;
    }

    public static void main(String[] args) {
        System.out.println("压缩图片开始...");
        File src = new File("/Users/hsc/Desktop/1.jpeg");
        File tag = new File("/Users/hsc/Desktop/1-2.jpeg");
        System.out.println("压缩前src size:" + src.length());
        reduceImg(src, tag, 1000, 1000, (Float) null);
        System.out.println("压缩后tag size:" + tag.length());
    }
}